package bstsetcode.linkedliststack;

import java.util.Random;

/**
 * 测试链表栈和数组栈的性能
 * @author noob
 * @version 1.0
 * @date 2021/3/15 9:42
 */
public class MainTest {
    // 测试使用stack运行opCount个push和pop操作所需要的时间，单位：秒
    private static double testStack(Stack<Integer> stack, int opCount){

        long startTime = System.nanoTime();

        Random random = new Random();
        for(int i = 0 ; i < opCount ; i ++) {
            stack.push(random.nextInt(Integer.MAX_VALUE));
        }
        for(int i = 0 ; i < opCount ; i ++) {
            stack.pop();
        }

        long endTime = System.nanoTime();

        return (endTime - startTime) / 1000000000.0;
    }

    public static void main(String[] args) {

        int opCount = 1000000;
        int[] data={100000,10000000};
        for(int n:data){
            ArrayStack<Integer> arrayStack = new ArrayStack<>();
            double time1 = testStack(arrayStack, n);
            System.out.println("ArrayStack, time: " + time1 + " s");

            LinkedListStack<Integer> linkedListStack = new LinkedListStack<>();
            double time2 = testStack(linkedListStack, n);
            System.out.println("LinkedListStack, time: " + time2 + " s");

            System.out.println("-------------------------");
        }



        // 其实这个时间比较很复杂，因为LinkedListStack中包含更多的new操作
//        ArrayStack, time: 0.0158119 s
//        LinkedListStack, time: 0.0138948 s
//                -------------------------
//                ArrayStack, time: 2.7792147 s
//        LinkedListStack, time: 4.3279898 s
//                -------------------------
//     根据数据规模，结果不一定每次都一样
    }


}
